package com.pet.user.controller;


import com.pet.api.dto.UserReviewInfoDTO;
import com.pet.common.domain.Result;
import com.pet.user.domain.dto.LoginFormDTO;
import com.pet.user.domain.dto.RegisterFormDTO;
import com.pet.user.domain.dto.UserInfoFormDTO;
import com.pet.user.domain.po.User;
import com.pet.user.domain.query.UserQuery;
import com.pet.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-06-27
 */
@RestController
@Api(tags = "用户服务相关接口")
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody @Validated LoginFormDTO loginFormDTO) {
        return userService.login(loginFormDTO);
    }

    @PostMapping("/admin/login")
    @ApiOperation("管理员登录")
    public Result adminLogin(@RequestBody @Validated LoginFormDTO loginFormDTO) {
        return userService.loginByAdmin(loginFormDTO);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody @Validated RegisterFormDTO registerFormDTO) {
        return userService.register(registerFormDTO);
    }

    @PostMapping("/admin/list")
    @ApiOperation("分页获取用户信息")
    public Result getUsersList(@RequestBody UserQuery userQuery) {
        return Result.success(userService.getUsersList(userQuery));
    }

    @PutMapping("/admin")
    @ApiOperation("修改用户信息")
    public Result updateUser(@RequestBody User user) {
        return Result.success(userService.updateById(user));
    }

    @DeleteMapping("/admin/{id}")
    @ApiOperation("删除用户信息")
    public Result deleteUser(@PathVariable @ApiParam(value = "用户id", required = true) Integer id) {
        return Result.success(userService.removeById(id));
    }

    @GetMapping
    @ApiOperation("用户查询个人信息")
    public Result getInfoByUser() {
        return userService.getInfoByUser();
    }

    @PutMapping
    @ApiOperation("用户修改个人信息")
    public Result updateInfoByUser(@RequestBody @Validated UserInfoFormDTO userInfoFormDTO) {
        return userService.updateInfoByUser(userInfoFormDTO);
    }

    @PutMapping("/points/pay")
    @ApiOperation(value = "用户付款后增加积分", hidden = true)
    public void addPointsByPay(@RequestParam("totalFee") Integer totalFee) {
        userService.addPointsByPay(totalFee);
    }

    @PostMapping("/getIdNickname")
    @ApiOperation(value = "根据用户id列表获取用户id和昵称列表", hidden = true)
    public Map<Integer, String> getIdNicknameMap(@RequestBody List<Integer> userIds) {
        return userService.getIdNicknameMap(userIds);
    }

    @PostMapping("/getUserReviewInfo")
    @ApiOperation(value = "根据用户id列表获取用户信息", hidden = true)
    Map<Integer, UserReviewInfoDTO> queryUserReviewInfo(@RequestBody List<Integer> userIds) {
        return userService.queryUserReviewInfo(userIds);
    }
}
